<?php 
namespace XFCore\traits\admin;
use lib\Examine;
use Exception;
/*
*其他相关配置 包含 公告管理  插件管理 工单管理 邮件群发 微信云端配置 在线更新
*/
trait OtherTrait{
    public function Noticestatus(){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        $row = $this->db->noticerow($id,'id',"`id`,`status`");
        if(empty($row))exitjson(-1,'数据不存在');
        $params = [
            'status'=>$row['status'] == 1 ? 0 : 1
        ];
        if($this->db->update('notice',$params,['id'=>$id])):
            exitjson(200,'更新状态成功！');
        endif;
        exitjson(-1,'更新失败！');
    }
    public function add_Notice($get,$post,$data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        Examine::checkRequiredParams($data);
        $data['addtime'] = $this->date;
        $params = [];
        foreach ($data as $k => $v) {
            if ($v !== '') {
                $params[$k] = $v;
            }
        }
        if($this->db->insert('notice',$params)):
                exitjson(200,'添加成功！');
        endif;
        exitjson(-1,'添加失败！');
    }
    public function edit_Notice($get , $post , $data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        unset($data["id"]);
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        if(empty($this->db->noticerow($id , 'id' , 'id')))exitjson(-1,'数据不存在！');
        Examine::checkRequiredParams($data);
        $params = [];
        foreach ($data as $k => $v) {
            if ($v !== '') {
                $params[$k] = $v;
            }
        }
        if($this->db->update('notice',$params , ['id'=>$id])):
                exitjson(200,'修改成功！');
        endif;
        exitjson(-1,'修改失败！');
    }
    public function Noticeremove(){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        if(empty($this->db->noticerow($id , 'id' , 'id')))exitjson(-1,'数据不存在！');
        if($this->db->delete("notice",['id'=>$id])):
            exitjson(200,'删除成功！');
        endif;
        exitjson(-1,'删除失败！');
    }
    public function add_down($get,$post,$data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        Examine::checkRequiredParams($data);
        $data['addtime'] = $this->date;
        $params = [];
        foreach ($data as $k => $v) {
            if ($v !== '') {
                $params[$k] = $v;
            }
        }
        if($this->db->insert('down',$params)):
                exitjson(200,'添加成功！');
        endif;
        exitjson(-1,'添加失败！');
    }
    public function edit_down($get , $post , $data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        unset($data["id"]);
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        if(empty($this->db->downrow($id , 'id' , 'id')))exitjson(-1,'数据不存在！');
        Examine::checkRequiredParams($data);
        
        $params = [];
        foreach ($data as $k => $v) {
            if ($v !== '') {
                $params[$k] = $v;
            }
        }
        if($this->db->update('down',$params , ['id'=>$id])):
            exitjson(200,'修改成功！');
        endif;
        exitjson(-1,'修改失败！');
    }
    public function downremove(){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        if(empty($this->db->downrow($id , 'id' , 'id')))exitjson(-1,'数据不存在！');
        if($this->db->delete("down",['id'=>$id])):
            exitjson(200,'删除成功！');
        endif;
        exitjson(-1,'删除失败！');
    }
    public function edit_Huifu($get , $post , $data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        $row = $this->db->workrow($id);
        if(empty($row))exitjson(-1,'工单数据不存在');
        if($row['active'] == 1)exitjson(-1,'该工单已回复请勿重复回复');
        Examine::checkRequiredParams($data);
        $userrow = $this->db->userrow($row['uid'],'pid','`email` , `user`');
        if(empty($userrow))exitjson(-1,'用户数据不存在');
        $email = $userrow['email'] ?: $row['qq'] . '@qq.com';
        if($this->db->update("work",['huifu'=>$data['huifu'] , 'wdata'=>$this->date , 'active'=>1] , ['id'=>$id])){
            $sub = $this->conf->sitename.' - 您提交的工单已有回复';
            $msg = $this->MsgNotice->send("work",$userrow['user'],$row);
            send_mail($email, $sub, $msg);
            exitjson(200,'回复成功');
        }
        exitjson(-1,'回复失败！');
        
    }
    public function workremove(){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        if(empty($this->db->workrow($id , 'id' , 'id')))exitjson(-1,'数据不存在！');
        if($this->db->delete("work",['id'=>$id])):
            exitjson(200,'删除成功！');
        endif;
        exitjson(-1,'删除失败！');
    }
    public function add_email($get , $post , $data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $data   = array_filter($data, function($value) {
            return $value !== null && $value !== '';
        });
        $user_type  = $data['user_type'];
        $mail_title = $data['mail_title'];
        $mail_content = $data['mail_content'];
        $data['time'] = $this->date;
        if(empty($user_type))exitjson(-1,'请选择发信类型！');
        if(empty($mail_title) || empty($mail_content))exitjson(-1,'发信标题和发信内容不可为空');
        $success_count = 0; // 成功发送的计数器
        $error_count = 0; // 发送失败的计数器
        $params = ['mailtitle'=>$mail_title , 'time'=>$this->date];
        switch($user_type){
            case '1':
                $rs = $this->db->select("user","email,pid",[]);
                if($rs['count'] > 0){
                    foreach($rs['result'] as $res){
                        if(!empty($res['email'])){
                            $email = $res['email'];
                            $params['mailname'] = $email;
                            $params['pid']  = $res['pid'];
                            try{
                                $is_sent_successfully = send_mail($email, $mail_title, $mail_content);
                                if ($is_sent_successfully) {
                                    $success_count++;
                                    $code = 1;
                                } else {
                                    $error_count++;
                                    $code = 0;
                                }
                                $params['type'] = $code;
                                
                            }catch (Exception $e) {
                                 $error_count++;
                                 $params['type'] = 0;
                            }
                        }
                        $this->db->insert('email',$params);
                    }
                    exitjson(200, "全部执行完毕，成功发信邮件{$success_count}个，发信失败邮件{$error_count}个。");
                }
            break;
            case '2':
                if(empty($data['mail_email']))exitjson(-1,'发信邮箱不可为空！');
                $email_arr = explode(',', $data['mail_email']);
                foreach ($email_arr as $email){
                    $params['mailname'] = $email;
                    $params['pid']  = '非平台用户';
                    if(filter_var($email, FILTER_VALIDATE_EMAIL)){
                        $is_sent_successfully = send_mail($email, $mail_title, $mail_content); 
                        if ($is_sent_successfully) {
                            $success_count++;
                            $code = 1;
                        } else {
                            $error_count++;
                            $code = 0;
                        }
                        $params['type'] = $code;
                        $this->db->insert('email',$params);
                    }
                }
                exitjson(200, "全部执行完毕，成功发信邮件{$success_count}个，发信失败邮件{$error_count}个。"); 
            break;
        }
        exitjson(-1,'发信失败!');
    }
     public function emailRemove(){
        if($this->islogin_admin!=1) exitjson(-1,"请先登录！");
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        if(empty($this->db->emailrow($id,'id','id')))exitjson(-1,'发信数据不存在');
        if($this->db->delete('email', ['id'=>$id])){
           exitjson(200,"删除发信记录成功！");
       }
       exitjson(-1,'删除失败！');
    }
    public function emailallRemove(){
        if($this->islogin_admin!=1) exitjson(-1,"请先登录！");
        $count = $this->db->select('email','count(*) as count',[],['single_row'=>true])['result']['count'];
       if(empty($count)){
           exitjson(-1,'一键删除发信记录失败 当前无日志！');
       }
       if($this->db->delete('email')){
            createLog('一键删除发信记录成功 共删除：'.$count.'个记录',http_ip(),'admin');
           exitjson(200,'一键删除发信记录成功！');
       }
       exitjson(-1,'删除失败！');
    }
    public function wxyunurlstatus(){
        if($this->islogin_admin!=1) exitjson(-1,"请先登录！");
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        $row = $this->db->wxyunurlrow($id,'id',"`status`,`id`");
        if(empty($row))exitjson(-1,'数据不存在！');
        $params = [
            'status'=>$row['status'] == 1 ? 0 : 1 ,
        ];
        if($this->db->update('wxyunurl',$params,['id'=>$id])):
                exitjson(200,'更新状态成功！');
        endif;
        exitjson(-1,'更新状态失败');
    }
    public function Add_wxyunurl($get  , $post , $data){
        if($this->islogin_admin!=1) exitjson(-1,"请先登录！");
        Examine::checkRequiredParams($data);
        if($this->db->wxyunurlrow($this->input->url,'url','id'))exitjson(-1,'该链接已存在请勿重复添加！');
        $params = [];
        foreach ($data as $k => $v) {
            if ($v !== '') {
                $params[$k] = $v;
            }
        }
        if($this->db->insert('wxyunurl',$params)):
                exitjson(200,'添加成功！');
        endif;
        exitjson(-1,'添加失败！');
    }
    public function Edit_wxyunurl($get ,$post , $data){
        if($this->islogin_admin!=1) exitjson(-1,"请先登录！");
        Examine::checkRequiredParams($data);
        unset($data['id']);
        $id = $this->input->id;
        $url = $this->input->url;
        $row = $this->db->wxyunurlrow($url,'url','id');
        if($row && $row['id'] != $id){
            exitjson(-1 , '该链接已存在请勿重复添加');
        }
        $params = [];
        foreach ($data as $k => $v) {
            if ($v !== '') {
                $params[$k] = $v;
            }
        }
        if($this->db->update('wxyunurl',$params,['id'=>$id])):
            exitjson(200,'修改成功！');
        endif;
        exitjson(-1,'修改失败');
    }
    public function wxyunurlremove(){
        if($this->islogin_admin!=1) exitjson(-1,"请先登录！");
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        $row = $this->db->wxyunurlrow($id,'id','`id`,`code`');
        if(empty($row)){
            exitjson(-1,'数据不存在');
        }
        if($this->db->delete('wxyunurl',['id'=>$id])):
            createLog('删除'.(wxyuntypetemp($row['code'])).'云端链接成功',http_ip(),'admin');
            exitjson(200,'删除成功！');
        endif;
        exitjson(-1,'删除失败！');
    }
    public function updatePwd($get , $data){
        if($this->islogin_admin!=1) exitjson(-1,"请先登录！");
        Examine::checkRequiredParams($data);
        $olduser = $data['olduser'];
        $newuser = $data['newuser'];
        $oldpwd  = $data['oldpwd'];
        $newpwd  = $data['newpwd'];
        $row = $this->db->adminrow($olduser,'user');
        if(empty($row)){
            exitjson(-1,'旧账号或密码错误！');
        }
        if(strlen($newpwd) < 6){
            exitjson(500, "密码长度不能少于6位");
        }
        if(ctype_digit($newpwd)){
            exitjson(500, "密码过于简单，请使用包含字母、数字和特殊字符的更复杂密码");
        }
        $pwd = md5($row['salt'].$oldpwd.$row['salt'].md5($row['salt'].$oldpwd.$row['salt'].$oldpwd.$row['salt']).$row['salt'].$oldpwd.$row['salt']);
        
        if($pwd!=$row['pwd']){
            exitjson(-1,'旧账号或密码错误');
        }
        $params = [];
        if($olduser != $newuser){
            $params['user'] = $newuser;
        }
        if($oldpwd != $newpwd){
            $salt = $this->salt;
            $params['pwd'] = md5($salt.$newpwd.$salt.md5($salt.$newpwd.$salt.$newpwd.$salt).$salt.$newpwd.$salt);
            $params['salt'] = $salt;
        }
        
        if(empty($params)){
            exitjson(-1,'请任意修改一项');
        }
        
        if($this->db->update("admin",$params,['id'=>$row['id']])){
            createLog('修改管理员账号密码成功',http_ip(),'admin');
            exitjson(200,'修改成功');
        }
        exitjson(-1,'修改失败！');
    }
    public function up(){
        if($this->islogin_admin!=1) exitjson(-1,"请先登录！");
        $res = $this->CurlUrl->update_api_check($this);
        if($res['code']!=1){
            exitjson(-1,'暂时无法更新');
        }
        $RemoteFile = $res['file'];
        $ZipFile = "updata.zip";
        download_zip($ZipFile,$RemoteFile);
        if (zipExtract($ZipFile,ROOT)){
            $up=get_zip_originalsize($ZipFile,ROOT);
              unlink($ZipFile);
            exitjson(1,"程序更新成功！(".$up.")");
        }else{
              if (file_exists($ZipFile)){
                //   unlink($ZipFile);
                  exitjson(-1,'无法解压文件');
              }
         }
    }
}
?>